package com.kh.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kh.reggie.common.CustomerException;
import com.kh.reggie.common.Result;
import com.kh.reggie.entity.Category;

import com.kh.reggie.entity.Dish;
import com.kh.reggie.entity.Setmeal;
import com.kh.reggie.mapper.CategoryMapper;

import com.kh.reggie.service.CategoryService;
import com.kh.reggie.service.DishService;
import com.kh.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service

public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    /**
     * 根据ID删除分类，删除前判断有没有关联的菜品/套餐
     * @param id
     * @return
     */
    @Override
    public Void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();

//        添加查询条件，根据ID查
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(dishLambdaQueryWrapper);
//        查询有没有关联的菜品，有则抛出异常
        if (count>0){
            throw new CustomerException("当前项有关联的菜品");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        添加查询条件，根据ID查
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(setmealLambdaQueryWrapper);

//        查询有没有关联的套餐，有则抛出异常
        if (count1>0){
            throw new CustomerException("当前项有关联的套餐");
        }
//        正常删除
        super.removeById(id);
        return null;
    }
}
